﻿using System.Reactive.Concurrency;
using KinonekoSoftware.Foundation.Styles;

namespace KinonekoSoftware.Foundation.UI
{
    public static partial class Xplat
    {
        private static ThemeSystem _theme;

        /// <summary>
        /// 
        /// </summary>
        public static ThemeSystem Theme
        {
            get
            {
                _theme ??= new DarkThemeSystem();
                return _theme;
            }
            set => _theme = value;
        }
        
        

        public static void RunOnUIThread(Action action)
        {
            if (action is null)
            {
                return;
            }

            if (UI is not null && SynchronizationContext.Current == UI)
            {
                action();
            }
            else
            {
                UI?.Send(_ => action(), null);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public static void SetupUIThread(SynchronizationContext context)
        {
            UI = context ?? throw new ArgumentNullException(nameof(context));
            
            Ioc.Use<IScheduler>(new SynchronizationContextScheduler(context));
        }

        
        /// <summary>
        /// 
        /// </summary>
        /// <remarks>在Application初始化时，赋值.</remarks>
        public static SynchronizationContext UI { get; private set; }

    }
}